/** **********
 * *注意事项*
 * *新增数据属性后 需要同步在reset中更新 📌
 * **********/

import { Subject } from "rxjs";
import { ArrayPlus, VTActor } from "cax-base";
import {
  MeshEdgeData,
  SelectedBackData,
  SelectedEdgeInfo,
  SelectedParts,
  VIEW_MODE,
} from "../interface";
import { observable } from "mobx";

export enum APP_STATUS_CODE {
  // * 0 普通视图状态
  NORMAL,
}

export enum STATUS_CODE {
  // * 0 正常状态
  NORMAL,
  // * 1 顶点测量
  VERTEX_MEASURE,
  // * 2  选中实体
  GEOM_BODY,
  // * 3 选中边
  EDGE,
  // * 4 固体切割
  TRIM,
  // * 5 顶点选择(点选网格点)
  VERTEX_SELECT,
  // 6 网格边选择
  MESH_EDGE,
  // 7 单点选择
  SINGLE_POINT_SELECT,
  //8 单线选择
  SINGLE_LINE_SELECT,
}

export enum SELECTOR_MODE {
  // * 0 几何顶点选择
  GEO_POINT_MODE,
  // * 1 几何边选择
  GEO_LINE_MODE,
  // * 2 几何面选择
  GEO_FACE_MODE,
}

export enum STATE_EVENT {
  CONNECT = "connect",
  DISCONNECT = "disconnect",
  ESCKEYDOWN = "escKeyDown",
}

export class State {
  @observable currentSelectedActorId = -1;
  currentSelectedPartId = -1;
  currentSelectedPartEntityId = -1;
  currentSelectedFaceId = -1;
  currentSelectedVertexId = -1;
  currentSelectedEdgeStartIndex = -1;
  currentSelectedEdgeEndIndex = -1;
  currentSelectedEdgeOriginColor = [0, 0, 0, 255];
  currentSelectedTriangleIds = new Set<number>();
  currentSelectedPartIds = new Set<number>();
  currentSelectedPartEntityIds = new Set<number>();
  currentDeletedParts: any = [];
  currentSelectedFaceIds = new Set<number>();
  currentSelectedVertexIds = new Set<any>();
  currentSelectedMeshPartId = -1;
  currentSelectedMeshId = -1;
  currentSelectedMeshIds = new Set<number>();
  // 当前选中的网格边
  currentSelectedMeshEdges = new Map<string, MeshEdgeData>();
  currentSelectedMeshPointId = -1;
  currentSelectedMeshPointIds = new Set<number>();
  currentSelectedMeshPointIdMapPartId = new Map<number, number>();
  // partId: [faceIds] 为了多个实体上多个面的删除
  // currentSelectedPartDict = new Map<string, any[]>();
  //包含了选中的所有实体、点、线、面信息
  currentSelectedParts: SelectedParts[] = [];
  currentSelectedActors: ArrayPlus<VTActor> = new ArrayPlus([]);
  currentSelectedBackData: SelectedBackData = {
    actorIndex: 0,
    faceId: 0,
    faceIndex: 0,
    triangleIds: [],
  };
  // 中面厚度网格数据，整体控制显示隐藏
  midSurfaceThicknessPointsData: number[][][] = [];
  midSurfaceThickness = 0;
  // * key 的组成由三个部分组成 partId_faceId_edgeId
  currentSelectedEdges = new Map<string, SelectedEdgeInfo>();
  maskFaceIds = new Set<number>();
  modeCode = STATUS_CODE.NORMAL;
  viewMode = VIEW_MODE.SURFACE; // 默认模式：显示面
  private connectSubject = new Subject<boolean>();
  private disconnectSubject = new Subject<boolean>();
  private keydownSubject = new Subject<boolean>();

  sub(event: STATE_EVENT, callback: any) {
    switch (event) {
      case STATE_EVENT.CONNECT:
        this.connectSubject.subscribe(callback);
        break;
      case STATE_EVENT.DISCONNECT:
        this.disconnectSubject.subscribe(callback);
        break;
      case STATE_EVENT.ESCKEYDOWN:
        this.keydownSubject.subscribe(callback);
    }
  }

  pub(event: string, data: any) {
    switch (event) {
      case STATE_EVENT.CONNECT:
        this.connectSubject.next(data);
        break;
      case STATE_EVENT.DISCONNECT:
        this.disconnectSubject.next(data);
        break;
      case STATE_EVENT.ESCKEYDOWN:
        this.keydownSubject.next(data);
    }
  }

  setViewMode(mode: VIEW_MODE) {
    this.viewMode = mode;
  }
  getViewMode() {
    return this.viewMode;
  }
  /**
   * @description 重置数据状态
   */
  reset() {
    this.currentSelectedActorId = -1;
    this.currentSelectedPartId = -1;
    this.currentSelectedFaceId = -1;
    this.currentSelectedVertexId = -1;
    this.currentSelectedEdgeStartIndex = -1;
    this.currentSelectedEdgeEndIndex = -1;
    this.currentSelectedEdgeOriginColor = [0, 0, 0, 255];
    this.currentSelectedTriangleIds = new Set<number>();
    this.currentSelectedPartIds = new Set<number>();
    this.currentSelectedPartEntityIds = new Set<number>();
    this.currentDeletedParts = [];
    this.currentSelectedFaceIds = new Set<number>();
    this.currentSelectedVertexIds = new Set<any>();
    this.currentSelectedMeshId = -1;
    this.currentSelectedMeshIds = new Set<number>();
    this.currentSelectedMeshPointId = -1;
    this.currentSelectedMeshPointIds = new Set<number>();
    this.currentSelectedMeshEdges = new Map<string, MeshEdgeData>();
    // partId: [faceIds] 为了多个实体上多个面的删除
    // currentSelectedPartDict = new Map<string, any[]>();
    this.currentSelectedParts = [];
    this.currentSelectedActors = new ArrayPlus([]);
    this.currentSelectedBackData = {
      actorIndex: 0,
      faceId: 0,
      faceIndex: 0,
      triangleIds: [],
    };
    this.maskFaceIds = new Set<number>();
    this.modeCode = STATUS_CODE.NORMAL;
    this.viewMode = VIEW_MODE.SURFACE;
  }

  // * 获取当前选中的网格ID
  getSelectedMeshIds(): number[] {
    return this.currentSelectedMeshIds.keys().toArray();
  }
}
